local skels = {}

local pinmu = fk.CreateSkill{
    name = "ym2__pinmu",
}
Fk:loadTranslationTable{
    ["ym2__pinmu"] = "牝牧",
    [":ym2__pinmu"] = "出牌阶段限一次，或当你受到伤害后，你可以视为使用【树上开花】且可以额外指定一名角色为目标。此牌结算后，若你与其弃置的花色组合一致，其翻面。",
    ["#ym2__pinmu"] = "牝牧：视为使用【树上开花】且可以额外指定一名角色为目标",

    ["$ym2__pinmu1"] = "不怕我父王杀了你们吗？",
    ["$ym2__pinmu2"] = "见到本公主，为何不跪？",
}
pinmu:addEffect("active",{
    prompt = "#ym2__pinmu",
    anim_type = "drawcard",
    mute = true,
    card_num = 0,
    card_filter = Util.FalseFunc,
    max_target_num = 1,
    min_target_num = 0,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        local c = Fk:cloneCard("bogus_flower")
        c.skillName = pinmu.name
        return #selected == 0 and (player:canUseTo(c,to_select) or to_select:canUseTo(c,to_select)) and player ~= to_select
    end,
    can_use = function (self, player)
        local c = Fk:cloneCard("bogus_flower")
        c.skillName = pinmu.name
        return player:usedSkillTimes(pinmu.name,Player.HistoryPhase) == 0 and not player:prohibitUse(c) and player:canUseTo(c,player)
    end,
    on_use = function (self, room, effect)
        local player = effect.from
        room:notifySkillInvoked(player,pinmu.name,"drawcard")
        player:broadcastSkillInvoke(pinmu.name)
        local tos = {player}
        if #effect.tos > 0 then
            table.insert(tos,effect.tos[1])
        end
        local use = room:useVirtualCard("bogus_flower", nil, player, tos, pinmu.name)
        if use and #effect.tos > 0 then
            local suits1, suits2 = use.extra_data.ym2__pinmu[player.id], use.extra_data.ym2__pinmu[effect.tos[1].id]
            local bool = true
            for _, suit in ipairs(suits1) do
                if not table.contains(suits2,suit) then
                    bool = false
                    break
                end
            end
            if #suits1 == #suits2 and bool then
                tos[2]:turnOver()
            end
        end
    end,
})
pinmu:addEffect(fk.Damaged,{
    can_refresh = function (self, event, target, player, data)
        return target == player and player:hasSkill(pinmu.name)
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:askToUseActiveSkill(player,{
            skill_name = pinmu.name,
            prompt = "#ym2__pinmu",
        })
    end,
})
pinmu:addEffect(fk.PreCardEffect,{
    can_refresh = function (self, event, target, player, data)
        return data and data.card.trueName == "bogus_flower" and data.card.skillName == pinmu.name
    end,
    on_refresh = function(self, event, target, player, data)
        local card = data.card:clone()
        local c = table.simpleClone(data.card)
        for k, v in pairs(c) do
            card[k] = v
        end
        card.skill = Fk.skills["ym2__pinmu_bogus_flower_skill"]
        data.card = card
    end,
})
table.insert(skels,pinmu)

local bogus_flower_skill = fk.CreateSkill{
    name = "ym2__pinmu_bogus_flower_skill",
}
bogus_flower_skill:addEffect("cardskill",{
    mute = true,
    mod_target_filter = Util.TrueFunc,
    can_use = Util.CanUseToSelf,
    on_effect = function(self, room, effect)
        local target = effect.to
        if target.dead or target:isNude() then return end
        local cards = room:askToDiscard(target, {
            min_num = 1,
            max_num = 2,
            include_equip = true,
            skill_name = "bogus_flower_skill",
            prompt = "#bogus_flower-discard",
            cancelable = false,
            skip = true,
        })
        if #cards == 0 then return end
        local n = #cards
        local suits = {}
        for _, id in ipairs(cards) do
            table.insertIfNeed(suits,Fk:getCardById(id).suit)
            if Fk:getCardById(id).type == Card.TypeEquip then
                n = #cards + 1
            end
        end
        local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if use_event then
            local use = use_event.data
            use.extra_data = use.extra_data or {}
            use.extra_data.ym2__pinmu = use.extra_data.ym2__pinmu or {}
            use.extra_data.ym2__pinmu[target.id] = suits
        end
        room:throwCard(cards, "bogus_flower_skill", target, target)
        if not target.dead then
            target:drawCards(n, "bogus_flower_skill")
        end
    end,
})
table.insert(skels,bogus_flower_skill)

return skels